# WeatherFetcher - Update all feeds to local cache. GUI reads feeds from cache.
# Copyright (C) 2007 Lauri Taimila
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

__licence__ = "GPLv2"
__copyright__ = "2007, Lauri Taimila"
__author__ = "Lauri Taimila <lauri@taimila.com>"

import os
import threading
from pysqlite2 import dbapi2 as sqlite

# Messaging system
from backend.core.message import Message
from backend.core.message_type_priority import MessageType
from backend.core.message_bus import MessageBus

class WeatherFetcher(threading.Thread):
    """Update weather information to the local weather cache database."""
    
    # Feed database file
    WEATHER_DB = os.path.expanduser('~/.entertainer/cache/weather.db')
    
    def __init__(self, message_bus, logger, location):
        """
        Create a new weather fetch thread.
        @param message_bus: MessageBus object
        @param logger: Logger object
        @param location: Location information (String)
        """
        threading.Thread.__init__(self)
        self.setName("WeatherFetcher")
        self.message_bus = message_bus
        self.logger = logger
        self.location = location
        
    def run(self):
        """
        Update weather cache in a seperated thread.
        """
        # Open weather database
        #db_conn = sqlite.connect(self.WEATHER_DB)
        #db_cursor = db_conn.cursor()
        
        #TODO: connect to net, download weather info, update downloaded info into cache
        #      Is SQLite cache overkill here? Maybe we should use file instead? Let's discuss :)
                    
        # Notify that weather database has been updated
        #db_conn.close()
        #self.logger.info("Weather cache has been updated")
        #self.message_bus.notifyMessage(Message(MessageType.WEATHER_INFO_UPDATED))
